package com.sokal.easycare.modules.sys.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sokal.easycare.common.config.Global;
import com.sokal.easycare.common.persistence.Page;
import com.sokal.easycare.common.utils.StringUtils;
import com.sokal.easycare.common.web.BaseController;
import com.sokal.easycare.modules.sys.entity.MainUser;
import com.sokal.easycare.modules.sys.entity.MainUser.UserType;
import com.sokal.easycare.modules.sys.entity.UserRealnameAuth;
import com.sokal.easycare.modules.sys.service.SystemService;
import com.sokal.easycare.modules.sys.utils.UserUtils;

/**
 * 用户Controller
 * 
 * @author yangmin
 * @version 2017-2-29
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/reguser")
public class RegUserController extends BaseController {

	@Autowired
	private SystemService systemService;

	@ModelAttribute
	public MainUser get(@RequestParam(required = false) String id) {
		if (StringUtils.isNotBlank(id)) {
			return systemService.getUser(id);
		} else {
			return new MainUser();
		}
	}

	@RequiresPermissions("sys:reguser:view")
	@RequestMapping(value = { "list", "" })
	public String list(MainUser user, HttpServletRequest request, HttpServletResponse response, Model model) {
		user.setUserType(UserType.REGISTERED_USER.value());
		Page<MainUser> page = systemService.findUser(new Page<MainUser>(request, response), user);
		model.addAttribute("page", page);
		return "modules/sys/regUserList";
	}

	@RequiresPermissions("sys:reguser:view")
	@RequestMapping(value = "form")
	public String form(MainUser user, Model model) {
		MainUser mainUser = UserUtils.getUser();
		if (user.getOffice() == null || user.getOffice().getId() == null) {
			user.setOffice(mainUser.getOffice());
		}
		model.addAttribute("user", user);
		return "modules/sys/userForm";
	}

	/**
	 * 用户信息显示及保存
	 * 
	 * @param user
	 * @param model
	 * @return
	 */
	@RequiresPermissions("sys:reguser:view")
	@RequestMapping(value = "info")
	public String info(MainUser user, HttpServletResponse response, Model model) {
		user = systemService.getUser(user.getId());
		model.addAttribute("user", user);
		model.addAttribute("fastdfs_file_url", Global.getConfig("fastdfs_file_url"));
		model.addAttribute("Global", new Global());
		return "modules/sys/regUserInfo";
	}

	/**
	 * 用户实名认证资料查看
	 * 
	 * @param user
	 * @param model
	 * @return
	 */
	@RequiresPermissions("sys:reguser:view")
	@RequestMapping(value = "authinfo")
	public String userRealnameAuthinfo(MainUser user, HttpServletResponse response, Model model) {
		user = systemService.getUser(user.getId());
		UserRealnameAuth userRealnameAuth = systemService.getUserRealnameAuth(user.getId());
		model.addAttribute("user", user);
		userRealnameAuth.getFile1();
		model.addAttribute("userRealnameAuth", userRealnameAuth);
		model.addAttribute("fastdfs_file_url", Global.getConfig("fastdfs_file_url"));
		model.addAttribute("Global", new Global());
		return "modules/sys/userRealnameAuthInfo";
	}

	/**
	 * 返回用户信息
	 * 
	 * @return
	 */
	@RequiresPermissions("user")
	@ResponseBody
	@RequestMapping(value = "infoData")
	public MainUser infoData() {
		return UserUtils.getUser();
	}
}
